package com.iims.market.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.market.domain.CmisOrder;
import com.iims.market.service.IOrderService;
import com.iims.market.vo.ContactRowVo;
import com.iims.oder.vo.request.GroupOrderSearchQuery;
import com.iims.oder.vo.resp.OrderDetailVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author fanpei
 * @date 2021/7/3 14:17
 */
@Api(tags = "I-订单管理")
@RestController
@RequestMapping("/business/order")
public class OrderController extends BaseController {

    @Autowired
    private IOrderService orderService;

    /**
     * 获取订单列表
     */
    @ApiOperation(value = "[高级搜索]分页查询订单列表", response = OrderDetailVo.class)
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "创建开始时间:format = \"yyyy-MM-dd\"", name = "beginTime", paramType = "query"),
            @ApiImplicitParam(value = "创建结束时间:format = \"yyyy-MM-dd\"", name = "endTime", paramType = "query"),
            @ApiImplicitParam(value = "订单状态,值来源于字典business_order_status", name = "orderStatus", paramType = "query"),
            @ApiImplicitParam(value = "业务类别,值来源于字典business_common_businessType", name = "businessType", paramType = "query"),
            @ApiImplicitParam(value = "产品频段,值来源于business_common_productBandType", name = "productBandType", paramType = "query"),
            @ApiImplicitParam(value = "订单编号:模糊匹配", name = "orderCode", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:order:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore CmisOrder order) throws Exception {
        startPage();
        setSearchTimeRange(order);
        setSeqNumPageSizeStart(order);
        return getDataTable(orderService.selectCmisOrderList(order));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "[分组搜索] 分页查询订单列表", notes = "按钮分组顺序：productBandType、businessType、orderStatus、timeValue",response = ContactRowVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "综合模糊搜索:订单编号/合同名/产品类型/产品名称", name = "searchValue", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:order:list')")
    @GetMapping("/groupList")
    public TableDataInfo groupList(GroupOrderSearchQuery groupSearchQuery) throws Exception {
        startPage();
        setSeqNumPageSizeStart(groupSearchQuery);
        return getDataTable(orderService.selectCmisOrderListByGroup(groupSearchQuery));
    }


    /**
     * [项目] 可选订单列表
     */
    @ApiOperation(value = "查询 [项目] 可选订单列表", response = OrderDetailVo.class)
    @ApiImplicitParam(value = "项目Id", name = "proId", paramType = "query", required = true)
    @GetMapping("/listByProEnable")
    public AjaxResult proEnableOrderList(@RequestParam Long proId) throws Exception {
        return AjaxResult.success(orderService.selectProEnableCmisOrderList(proId));
    }

    /**
     * 根据订单编号获取详细信息
     */
    @ApiOperation(value = "根据订单编号获取详细信息", response = OrderDetailVo.class)
    @ApiImplicitParam(name = "orderId", value = "订单Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable Long orderId) {
        return AjaxResult.success(orderService.selectDetailById(orderId));
    }

    /**
     * 新增订单
     */
    @ApiOperation(value = "新增订单", notes = "订单的收货人信息，默认填充为查询得到的订单收货人信息，用户可自行更改。查询接口为/business/contract/consignee/{contractId}")
    @ApiOperationSupport(ignoreParameters = {"orderId", "orderCode", "orderStatus"})
    @PreAuthorize("@ss.hasPermi('business:order:add')")
    @Log(title = "新增订单", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmisOrder order) {
        return toAjax(orderService.insertCmisOrder(order));
    }

    /**
     * 修改订单
     */
    @ApiOperation("修改订单")
    @PreAuthorize("@ss.hasPermi('business:order:edit')")
    @ApiOperationSupport(ignoreParameters = {"orderCode", "orderStatus"})
    @Log(title = "修改订单", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CmisOrder order) {
        return toAjax(orderService.updateCmisOrder(order));
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除订单")
    @PreAuthorize("@ss.hasPermi('business:order:remove')")
    @Log(title = "删除订单", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{orderId}")
    public AjaxResult remove(@ApiParam("订单Id") @PathVariable Long orderId) {
        if (orderService.checkCmisOrderUse(orderId)) {
            return AjaxResult.error("删除订单失败，订单已被使用！");
        }
        return toAjax(orderService.deleteCmisOrderById(orderId));
    }
}
